<?php
/**
 * 得点計算機
 * 
 * @author tanaka
 * @package Controller
 * @subpackage Score
 */
class ScoreController extends Controller_Action
{
	private $_container;
	
	private $_isValid = false;
	
	/**
	 * (non-PHPdoc)
	 * @see Zend_Controller_Action::preDispatch()
	 */
	public function preDispatch()
	{
		$this->_setViewLayoutFile('nomenu');
		$this->_container = new Container_Score($this->_getAllParams());
	}
	
	/**
	 * index
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		$masterJob = new Db_Master_Job();
		$this->view->majorJobs = $masterJob->getAllMajor();
		$this->view->minorJobs = $masterJob->getAllMinorStructed();
		$this->view->params = $this->_container;
		
		if(!$this->_container->isInputed()){
			return;
		}
		
		$form = new Form_Score($this->_container);
		
		if(!$form->isValid()){
			$this->_addErrorMessage($form->getMessages());
			return;
		}
		
		$this->_isValid = true;
		$this->view->result = $this->_getCalcResult($this->_container);
		$this->_addStdMessage('計算完了');
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Controller_Action::postDispatch()
	 */
	public function postDispatch()
	{
		$this->view->isValid = $this->_isValid;
		parent::postDispatch();
	}
	
	/**
	 * 計算結果
	 * 
	 * @param Container_Score $container
	 * @return array
	 */
	private function _getCalcResult(Container_Score $container)
	{
		$masterJob = new Db_Master_Job();
		
		$result = array();
		$result['major_job'] = $masterJob->getMajor($container->major_job);
		$result['minor_job'] = $masterJob->getMinor($container->major_job, $container->minor_job);
		
		$masterScore = new Db_Master_JobScore();
		$score = $masterScore->getDefaultByPrimary($container->major_job, $container->minor_job);
		
		$totalPC = $container->pc_damage * $score['DAMAGE_RATE'];
		$result['pc_damage'] = $container->pc_damage . " × " . $score['DAMAGE_RATE'] . "(職業別レート) ＝ " . $totalPC;
		
		$result['bonus'] = $score['BONUS'];
		
		$totalKill = $container->kill *1000;
		$result['kill'] = $container->kill . " × 1000 ＝ " . $totalKill;
		
		$totalDead = $container->dead * -3000;
		$result['dead'] = $container->dead . " × -3000 ＝ " . $totalDead;
		
		$totalBuild = intval($container->build_damage / 3 );
		$result['build_damage'] = $container->build_damage . " ÷ 3 ＝ " . $totalBuild;
		
		$totalContribute = intval($container->contribute / 20 * 1000);
		$result['contribute'] = $container->contribute . " ÷ 20 × 1000 ＝ " . $totalContribute;
		
		$total = $totalPC + $totalKill + $totalDead + $totalBuild + $totalContribute + $score['BONUS'];
		$result['total'] = 
			$totalPC .  " ＋ (" .  
			$score['BONUS'] . ") ＋ " . 
			$totalKill.  " ＋ (" . $totalDead .  ") ＋ " . 
			$totalBuild . " ＋ " . 
			$totalContribute . ' ＝ ' . $total;
		
		$result['rank'] = $this->_getRank($total);
		
		return $result;
	}
	
	private function _getRank($totalScore)
	{
		if($totalScore >= 25000){
			return 'ランクS';
		}
		
		if($totalScore >= 20000){
			return 'ランクA';
		}
		
		if($totalScore >= 15000){
			return 'ランクB';
		}
		
		if($totalScore >= 10000){
			return 'ランクC';
		}
		
		return 'ランクD';
	}
}